import re

def check_punctuation_rules(article_sections, extract_sections):
    paragraph_length = []
    for item in article_sections:
        paragraph_length.append(len(item) + 1)
    is_judgment = False
    errors = []
    total_length = 0
    # 读取裁判文书文本
    # with open(file_path, 'r', encoding='utf-8') as file:
    for paragraph_index, line in enumerate(article_sections):
        if paragraph_index != 0:
            total_length += paragraph_length[paragraph_index - 1]
        paragraph_index = paragraph_index + 1
        # 规则一：“被告辩称”“本院认为”等词语之后用逗号
        rule1_patterns = [
            r'被告辩称[^，]',  # "被告辩称" 后面不是逗号
            r'本院认为[^，]'  # "本院认为" 后面不是逗号
        ]

        # 规则二：“×××向本院提出诉讼请求”“本院认定如下”“判决如下”“裁定如下”等词语之后用冒号
        rule2_patterns = [
            r'提出诉讼请求[^：]',  # "提出诉讼请求" 后面不是冒号
            r'本院认定如下[^：]',  # "本院认定如下" 后面不是冒号
            r'判决如下[^：]',  # "判决如下" 后面不是冒号
            r'裁定如下[^：]'  # "裁定如下" 后面不是冒号
        ]

        # 规则三：裁判项序号后用顿号
        chinese_num_pattern = r'[零一二三四五六七八九十百千万亿]'
        rule3_pattern = [r'^[零一二三四五六七八九十百千万亿]+(?!、).*']  # 数字后面不是顿号
        # errors=[]
        # # 检查规则一
        # for pattern in rule1_patterns:
        #     matches = re.finditer(pattern, court_text)
        #     for match in matches:
        #         errors.append(f"Rule 1 error at position {match.start()}: '{match.group()}' should be followed by a comma.")
        #
        # # 检查规则二
        # for pattern in rule2_patterns:
        #     matches = re.finditer(pattern, court_text)
        #     for match in matches:
        #         errors.append(f"Rule 2 error at position {match.start()}: '{match.group()}' should be followed by a colon.")
        #
        # # 检查规则三
        # matches = re.finditer(rule3_pattern, court_text)
        # for match in matches:
        #     errors.append(f"Rule 3 error at position {match.start()}: '{match.group()}' should be followed by a pause mark.")
        #
        # return errors
        # 检查规则一：错误示例，假设错误是“错误词”应该跟在逗号后
        rule1_error_word = "标点错误"
        for pattern in rule1_patterns:
            matches = re.finditer(pattern, line)
            for match in matches:
                errors.append({
                    "correct": "",
                    # "desc": f"'{match.group()}'后应用逗号",
                    "desc": "",
                    "end": total_length + match.end(),
                    "exception": "False",
                    "explain": "",
                    "label": "",
                    "label_type": "",
                    "offset": len(match.group())-1,
                    "opt": "",
                    "ppl": "",
                    "pre_insert": "",
                    "source": "",
                    "start": total_length + match.start(),
                    "tag": "",
                    "text": "",
                    "type": f"{match.group()[:len(match.group())-1]}后应用逗号",
                    "url": "",
                    "word": "",
                    "paragraph_index": paragraph_index
                })

        # 检查规则二：错误示例，假设错误是“错误词”应该跟在冒号后
        rule2_error_word = "标点错误"
        for pattern in rule2_patterns:
            matches = re.finditer(pattern, line)
            for match in matches:
                errors.append({
                    "correct": "",
                    "desc": "",
                    "end": total_length + match.end(),
                    "exception": "False",
                    "explain": f"{match.group()[:len(match.group())-1]}后应用冒号",
                    "label": "",
                    "label_type": "",
                    "offset": len(match.group())-1,
                    "opt": "",
                    "ppl": "",
                    "pre_insert": 1,
                    "source": "",
                    "start": total_length + match.start(),
                    "tag": "",
                    "text": "",
                    "type": f"{match.group()[:len(match.group())-1]}后应用冒号",
                    "url": "",
                    "word": "",
                    "paragraph_index": paragraph_index
                })

        # 检查规则三：错误示例，假设错误是“错误词”应该跟在暂停标记后
        if is_judgment and not line.startswith(('一', '二', '三', '四', '五', '六', '七', '八', '九', '十')):
            is_judgment = False
        if is_judgment:
            rule3_error_word = "标点错误"
            for pattern in rule3_pattern:
                matches = re.finditer(pattern, line)
                for match in matches:
                    errors.append({
                        "correct": "",
                        "desc": "",
                        "end": total_length + match.end(),
                        "exception": "False",
                        "explain": "",
                        "label": "",
                        "label_type": "",
                        "offset": len(match.group())-1,
                        "opt": "",
                        "ppl": "",
                        "pre_insert": "",
                        "source": "",
                        "start": total_length + match.start(),
                        "tag": "",
                        "text": "",
                        "type": f"{match.group()[:len(match.group())-1]}后应用顿号",
                        "url": "",
                        "word": "",
                        # "paragraph_index": paragraph_index
                    })
        if line.endswith(('判决如下：', '判决如下:', '裁定如下：', '裁定如下:', '本院认定如下:', '本院认定如下：')):
        # if line.endswith(('判决如下：\n', '判决如下:\n', '裁定如下：\n', '裁定如下:\n', '本院认定如下:\n', '本院认定如下：\n')):
            is_judgment = True
    return {"data": errors}

if __name__ == "__main__":
    # 示例使用
    file_path = '/Users/hoyo/PycharmProjects/judicial_document_proofreading/enetity_extraction/0725_test_data/2-贵州林荣资产管理有限公司与刘正超、周相波民间借贷纠纷一审民事案141382473.txt'
    # 检查文本并获取格式化后的错误
    formatted_errors = check_punctuation_rules(file_path)

    # 打印格式化的错误信息
    print(formatted_errors)
